package services;

import java.util.Collection;

import javax.transaction.Transactional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import repositories.ShoppingCartItemUnitRepository;
import domain.AvailableItem;
import domain.Customer;
import domain.ShoppingCartItemUnit;

@Service
@Transactional
public class ShoppingCartItemUnitService {

	// Managed Repository
	@Autowired
	private ShoppingCartItemUnitRepository shoppingCartItemUnitRepository;
	// SupportingServices
	@Autowired
	private CustomerService customerService;

	// Constructors methods
	public ShoppingCartItemUnitService() {
		super();
	}

	// Simple CRUD methods
	public ShoppingCartItemUnit create(AvailableItem availableItem) {
		Assert.notNull(availableItem);
		ShoppingCartItemUnit res;
		Customer principal;

		principal = customerService.findByPrincipal();

		res = new ShoppingCartItemUnit();
		res.setNumberOfItems(1);
		res.setCustomer(principal);
		res.setAvailableItem(availableItem);

		return res;
	}

	public ShoppingCartItemUnit save(ShoppingCartItemUnit sciu) {
		ShoppingCartItemUnit res;
		Customer principal;

		Assert.notNull(sciu);
		principal = customerService.findByPrincipal();

		Assert.isTrue(sciu.getCustomer().equals(principal));

		res = shoppingCartItemUnitRepository.save(sciu);

		return res;
	}

	public ShoppingCartItemUnit findOne(int shoppingCartItemUnitId) {
		ShoppingCartItemUnit result;

		result = shoppingCartItemUnitRepository.findOne(shoppingCartItemUnitId);

		return result;
	}

	public Collection<ShoppingCartItemUnit> findAll() {
		Collection<ShoppingCartItemUnit> result;

		result = shoppingCartItemUnitRepository.findAll();

		return result;
	}

	public void delete(ShoppingCartItemUnit sciu) {
		Assert.notNull(sciu);
		Customer principal;

		principal = customerService.findByPrincipal();

		Assert.isTrue(sciu.getCustomer().equals(principal));

		shoppingCartItemUnitRepository.delete(sciu);
	}

	public void deleteBecauseItemDelete(ShoppingCartItemUnit sciu) {
		Assert.notNull(sciu);

		shoppingCartItemUnitRepository.delete(sciu);
	}

	// Other business methods

	public ShoppingCartItemUnit findByCustomerAndItem(
			AvailableItem availableItem) {
		Assert.notNull(availableItem);
		ShoppingCartItemUnit res;
		Customer principal;

		principal = customerService.findByPrincipal();

		res = shoppingCartItemUnitRepository.findByCustomerAndItemId(
				principal.getId(), availableItem.getId());

		return res;
	}

	public Collection<ShoppingCartItemUnit> findByCustomer() {
		Customer principal;
		Collection<ShoppingCartItemUnit> res;

		principal = customerService.findByPrincipal();

		res = shoppingCartItemUnitRepository
				.findByCustomerId(principal.getId());

		return res;
	}

	public void changeUntisAndSave(ShoppingCartItemUnit shoppingCartItemUnit) {
		Customer principal;

		principal = customerService.findByPrincipal();
		Assert.isTrue(shoppingCartItemUnit.getCustomer().equals(principal));
		Assert.notNull(shoppingCartItemUnit);

		if (shoppingCartItemUnit.getNumberOfItems() == 0) {
			delete(shoppingCartItemUnit);
		} else {

			Assert.isTrue(shoppingCartItemUnit.getAvailableItem()
					.getAvailableUnits() >= shoppingCartItemUnit
					.getNumberOfItems());

			shoppingCartItemUnit.setNumberOfItems(shoppingCartItemUnit
					.getNumberOfItems());
			save(shoppingCartItemUnit);
		}
	}
}
